package cn.woblog.recyclerviewsample;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import java.util.ArrayList;
import java.util.List;

public class WaterfallFlowActivity extends AppCompatActivity {

  private RecyclerView rv;

  private List<String> data = new ArrayList<>();

  public static List<String> getTestImage() {
    ArrayList<String> strings = new ArrayList<>();
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037235_3453.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037235_7476.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037235_9280.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037234_3539.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037234_6318.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037194_2965.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037193_1687.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037193_1286.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037192_8379.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037178_9374.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037177_1254.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037177_6203.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037152_6352.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037151_9565.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037151_7904.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037148_7104.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037129_8825.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037128_5291.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037128_3531.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037127_1085.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037095_7515.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037094_8001.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037093_7168.jpg");
    strings.add("http://img.my.csdn.net/uploads/201309/01/1378037091_4950.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949643_6410.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949642_6939.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949630_4505.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949630_4593.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949629_7309.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949629_8247.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949615_1986.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949614_8482.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949614_3743.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949614_4199.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949599_3416.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949599_5269.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949598_7858.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949598_9982.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949578_2770.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949578_8744.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949577_5210.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949577_1998.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949482_8813.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949481_6577.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949480_4490.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949455_6792.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949455_6345.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949442_4553.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949441_8987.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949441_5454.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949454_6367.jpg");
    strings.add("http://img.my.csdn.net/uploads/201308/31/1377949442_4562.j");
    return strings;
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_waterfall_flow);

    data.addAll(getTestImage());

    rv = (RecyclerView) findViewById(R.id.rv);

StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3,
  LinearLayoutCompat.VERTICAL);
rv.setLayoutManager(layoutManager);
//    rv.addItemDecoration(new StaggeredGridDivider());

    //设置适配器
    MyAdapter myAdapter = new MyAdapter(this);
    rv.setAdapter(myAdapter);
  }

  class MyAdapter extends RecyclerView.Adapter<WaterfallFlowActivity.MyAdapter.MyViewHolder> {

    private final LayoutInflater layoutInflater;
    private int[] heights = new int[data.size()];
    public MyAdapter(Context context) {
      layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public WaterfallFlowActivity.MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
        int viewType) {
      return new WaterfallFlowActivity.MyAdapter.MyViewHolder(
          layoutInflater.inflate(R.layout.item_water_flow, parent, false));
    }

    @Override
    public void onBindViewHolder(WaterfallFlowActivity.MyAdapter.MyViewHolder holder,
        int position) {
      String d = WaterfallFlowActivity.this.data.get(position);
      holder.bindData(d, position);
      holder.tv_text.setText("item:" + position);
    }

    @Override
    public int getItemCount() {
      return data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

      private final ImageView iv;
      private final TextView tv_text;

      public MyViewHolder(View itemView) {
        super(itemView);
        iv = (ImageView) itemView.findViewById(R.id.iv);
        tv_text = (TextView) itemView.findViewById(R.id.tv_text);
      }

      public void bindData(String d, final int position) {
        //如果有高度，就直接取出来不用再计算了
        final Integer height = heights[position];
        if (height == 0) {
          //没有高度，需要计算
          Glide.with(WaterfallFlowActivity.this).load(d).diskCacheStrategy(DiskCacheStrategy.ALL)
              .into(new SimpleTarget<GlideDrawable>() {
                @Override
                public void onResourceReady(GlideDrawable resource,
                    GlideAnimation<? super GlideDrawable> glideAnimation) {
                  Log.d("TAG", iv.getWidth() + "," + resource.getIntrinsicWidth());
                  //计算ImageView的高度
                  int imageWidth = resource.getIntrinsicWidth();
                  int imageHeight = resource.getIntrinsicHeight();
                  int imageViewWidth = iv.getWidth();

                  double scale = imageWidth * 1.0 / imageViewWidth;
                  LayoutParams layoutParams = iv.getLayoutParams();
                  int h = (int) (imageHeight / scale);
                  layoutParams.height = h;
                  iv.setLayoutParams(layoutParams);
                  iv.setImageDrawable(resource);

                  heights[position] = h;
                }
              });
        } else {
          //已经计算了，直接拿出来用
          LayoutParams layoutParams = iv.getLayoutParams();
          layoutParams.height = height;
          iv.setLayoutParams(layoutParams);

          Glide.with(WaterfallFlowActivity.this).load(d).diskCacheStrategy(DiskCacheStrategy.ALL)
              .into(iv);
        }
      }
    }
  }

}
